/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package htmlunit.code;

import com.gargoylesoftware.htmlunit.IncorrectnessListener;
import com.gargoylesoftware.htmlunit.SilentCssErrorHandler;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.DomElement;
import com.gargoylesoftware.htmlunit.html.HtmlDivision;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.javascript.JavaScriptErrorListener;
import hibernate.Dao.crawlerDao;
import java.net.URLDecoder;
import java.util.logging.Level;
import java.util.logging.Logger;
import htmlunit.mode.*;
import java.io.UnsupportedEncodingException;


/**
 *
 * @author TiTa
 */
public class parse_google_htmlUnit extends Thread implements IncorrectnessListener {
    /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

Link l_cn=new Link();



/**
 *
 * @author TiTa
 */
    crawlerDao cd=new crawlerDao();
            kiem_tra_loi_404 k404=new kiem_tra_loi_404();
    DomElement doc;
   // Queue<String> queue=new LinkedList<String>();
  //private queue_url queue;// loi
 queue_url queue=new queue_url();// ko loi 
 //  public parse_google_htmlUnit(queue_url que){

 //parse_google_htmlUnit(queue_url que) {
        int a;
  //     this.queue=que;
  // }
  private boolean paused;
    private boolean running;
 public parse_google_htmlUnit() {
     
       //  parse_page_google("https://www.google.com.vn/search?q=site:facebook.com+cafe+tphcm");
    }
public void Stop() {
        running = false;
        if (paused)
            interrupt();
        try {
            join();
        } catch (InterruptedException ex) {
        }
    }
       public void Pause() {
        paused = true;
    }

    public void Resume() {      
        interrupt();
        paused = false;
    }
    
    public parse_google_htmlUnit(queue_url qu) {
          this.queue=qu;
       // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

   public parse_google_htmlUnit(queue_url qu,int a1) {
          this.queue=qu;
          this.a=a1;
           paused = false;
        running = true;
    }

   
    
 //   que =queue;
   // }
   @Override
  public void run(){while (running) {//running =true
            while (!paused){//pause=false
                try {
                    Thread.sleep(500);
       // try {
        //    Thread.sleep(500);
      //  } catch (InterruptedException ex) {
       //     Logger.getLogger(parse_google_htmlUnit.class.getName()).log(Level.SEVERE, null, ex);
     //   }
                    System.out.println("running_  parse_page_google");
                  //  String url_one_main="https://www.google.com.vn/search?q=site:facebook.com+cafe+tphcm";
                    String pepe= "https://www.google.com.vn/search?q=site:facebook.com+qu%C3%A1n+%C4%83n+tphcm";
                 //  String pepe=URLDecoder.decode("https://www.google.com.vn/search?q=site:facebook.com+quán+ăn+tphcm","UTF-8");
                   System.out.print(pepe);
                    parse_page_google(pepe);
                    Pause();
                    Stop();
                    
                     } catch (InterruptedException e) {
                    paused = true;
                   //     } catch (UnsupportedEncodingException ex) {//moi them add catch
                 //   Logger.getLogger(parse_google_htmlUnit.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
  
            }
      try {
          Thread.currentThread().sleep(100);
        } catch (InterruptedException ex) {
           Logger.getLogger(parse_google_htmlUnit.class.getName()).log(Level.SEVERE, null, ex);
       }
                   
   
   }
  
    public  void  parse_page_google(String url)
    {
        
      //  String link;;
         
         //  String queue_trave;
     try{
     //    !-- tat het canh bao||( Warning)
         java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(java.util.logging.Level.OFF);
         java.util.logging.Logger.getLogger("org.apache.http").setLevel(java.util.logging.Level.OFF);
         java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(java.util.logging.Level.OFF);
     //--!end warning
         
    final WebClient webClient = new WebClient();
 // webClient.waitForBackgroundJavaScript(10000); moi bo
 // com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify = null;
  //webClient.setJavaScriptErrorListener((JavaScriptErrorListener) notify);
  webClient.getOptions().setUseInsecureSSL(true);// dung bo chung thuc
    webClient.getCookieManager().setCookiesEnabled(true);
    webClient.setCssErrorHandler(new SilentCssErrorHandler());//bo wa canh bao css
    webClient.setIncorrectnessListener(new IncorrectnessListener(){
	        		public void notify(String str,Object obj){}});
//webClient.getJavaScriptEngine().getContextFactory().setDebugger(new DebuggerImpl()); ??????
  //webClient.setIncorrectnessListene();
//  webClient.setCssErrorHandler();
 // webClient.setThrowExceptionOnScriptError(false); 
  for(int j=0;j<a;j+=10){
          //  url="https://www.google.com.vn/search?q=site:facebook.com+cafe&prmd=ivnsm&ei=y4nBUq-QNcW6lAWNsIGYAw&start="+j+"&sa=N";
             String foot_google=url+"&start="+j+"";
            final HtmlPage page = webClient.getPage(foot_google);
   // final HtmlPage page = webClient.getPage(url);
           // for(int j=0;j<650;j++){
           // url="https://www.google.com.vn/search?q=site:facebook.com+cafe&prmd=ivnsm&ei=y4nBUq-QNcW6lAWNsIGYAw&start="+j+"&sa=N";

    final HtmlDivision div = (HtmlDivision) page.getByXPath("//div[@id='ires']").get(0);
  
   
     for(int i=0;i<10;i++)
    {   
    doc=div.getElementsByTagName("h3").get(i);  
    String text_a1=URLDecoder.decode(doc.getElementsByTagName("a").get(0).getTextContent(),"UTF-8");
    
     String link1=doc.getElementsByTagName("a").get(0).getAttribute("href");
   
      String  link=doc.getElementsByTagName("a").get(0).getAttribute("href").substring(7, cat_theo_ki_tu(link1));
    /// phan qeue
      String enc=URLDecoder.decode(link,"UTF-8");//duong linh
      
      String a;//???
  if(k404.exists(enc)==true ){a="Đang hoạt động";}else{a="không hoạt động";}
  // insert to database
  l_cn.setDuongLink(enc);
  l_cn.setTenWeb(URLDecoder.decode(text_a1,"ISO-8859-1"));
  l_cn.setTinhTrang(URLDecoder.decode(a,"UTF-8"));
 // if(cd.capnhat(l_cn)){i=i+1;}
  
  cd.capnhat(l_cn);
  
         queue.tra(enc);
         
         lay_hinh_chua_thamso lay_hinh=new lay_hinh_chua_thamso(l_cn);
         lay_hinh.lay_hinh_khong_trang(enc);
       //  thread_queue tq=new thread_queue(l_cn);
       //  tq.start();
          parse_page_facebook ppf=new parse_page_facebook(l_cn);//bo vao ok
          ppf.parse_facebook_is_page_fbLongBlurb(enc);
        /*  try {
            Thread.currentThread().sleep(10000);
        } catch (InterruptedException ex) {
           // Logger.getLogger(parse_google_htmlUnit.class.getName()).log(Level.SEVERE, null, ex);
        }*/
         
         
    System.out.println(URLDecoder.decode(text_a1, "UTF-8") +"\n"+enc);
    }
    //   for(int j=0;j<10;j++)
 //   {
  // 
    //System.out.println("queue parse google"+"\n"+queue.poll());  
       
    }
       
 // }
    /* /// lay footer 
     final HtmlTable link_footer= (HtmlTable) page.getByXPath("//table[@id='nav']").get(0);
      // String the_td=link_footer.getElementsByTagName("td").get(2).getTextContent();//ra so trang
    String link_foot=link_footer.getElementsByTagName("a").get(0).getTextContent();
    String link1=link_footer.getElementsByTagName("a").get(0).getAttribute("href");
        System.out.println(link_foot+"\n"+link1);
        //end lay footer
        * */
      
        webClient.closeAllWindows();
   
   }
        catch(Exception  ex){
        ex.printStackTrace();}
      // queue_trave= queue.poll();
       // return queue_trave;
    }
    public int cat_theo_ki_tu(String input)
    {
         String[] arr;
       arr= input.split("");
       
       int tong =0;
       for(int i=1;i<input.length()+1;i++)
        {
           if(arr[i].equals("&"))
                   break;
            else
                tong+=1;
         }
              
        return tong;
    }   
  
    
    public static void main(String[] args){
       
        
    parse_google_htmlUnit ps=new parse_google_htmlUnit();
    String url_one_main="https://www.google.com.vn/search?q=site:facebook.com+quan an+tphcm";
    // String url_parse="https://www.google.com.vn/search?q=site:facebook.com+cafe&prmd=ivnsm&ei=9YTBUrvAOojbkgW_wYGwAw&start=10&sa=Ne";
     ps.parse_page_google(url_one_main);
 //    kiemtra_net kt=new kiemtra_net();
//        String url_foot="google.com.vn/search?q=site:facebook.com+cafe&prmd=ivnsm&ei=pbO_UvTUJYGFoQSvpoHYCQ&start=10&sa=N";
      
      // System.out.println(kt.kiemtra(url_foot)+"\n"); gia tri tra ve la false vi timeout lau wa
        
       /*  System.out.println(
        "Online: " +
        (kt.kiemtra("google.com.vn") || kt.kiemtra("amazon.com"))
    );*/
    }
@Override 
  public void notify(String arg0, Object arg1) 
  { 
    //System.err.println("Argument : " + arg0.toString() + ", Object : "); 
  } 


}
